unit uProcessThd;

interface

uses
  SysUtils, Classes;

type
  TProcWithParam = procedure (AParam: Variant) of object;
  TProcessThd = class(TThread)
  private
    FParamCnt: Integer;
    FParam: Variant;
    FProcessProc: TThreadMethod;
    FProcWithParam: TProcWithParam;
  protected
    procedure Execute; override;
  public
    constructor Create(AProcessProc: TThreadMethod); overload;
    constructor Create(ProcWithParam: TProcWithParam; AParam: Variant); overload;
    destructor Destroy; override;
  end;

  function RunInThread(ProcessProc: TThreadMethod): TProcessThd; overload;
  function RunInThread(ProcWithParam: TProcWithParam; AParam: Variant): TProcessThd; overload;

implementation

function RunInThread(ProcessProc: TThreadMethod): TProcessThd;
begin
  Result := TProcessThd.Create(ProcessProc);
  Result.Resume;
end;

function RunInThread(ProcWithParam: TProcWithParam;
  AParam: Variant): TProcessThd;
begin
  Result := TProcessThd.Create(ProcWithParam, AParam);
  Result.Resume;
end;

{ TProcessThd }

constructor TProcessThd.Create(AProcessProc: TThreadMethod);
begin
  FParamCnt := 0;
  FreeOnTerminate := True;
  FProcessProc := AProcessProc;

  inherited Create(True);
end;

constructor TProcessThd.Create(ProcWithParam: TProcWithParam; AParam: Variant);
begin
  FParamCnt := 1;
  FreeOnTerminate := True;
  FProcWithParam := ProcWithParam;
  FParam := AParam;

  inherited Create(True);
end;

destructor TProcessThd.Destroy;
begin

  inherited;
end;

procedure TProcessThd.Execute;
begin
  try
    case FParamCnt of
      0: FProcessProc();
      1: FProcWithParam(FParam);
    end;
  except
    //
  end;
end;

end.
